<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="prev" href="match-objects.html" />
<link rel="parent" href="module-re.html" />
<link rel="next" href="module-struct.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>4.2.6 例子</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="4.2.5 match Objects"
  href="match-objects.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="4.2 re  "
  href="module-re.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="4.3 struct  "
  href="module-struct.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python库参考</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">前一节:</b>
<a class="sectref" rel="prev" href="match-objects.html">4.2.5 匹配对象</a>
<b class="navlabel">上一级:</b>
<a class="sectref" rel="parent" href="module-re.html">4.2 re  </a>
<b class="navlabel">下一节:</b>
<a class="sectref" rel="next" href="module-struct.html">4.3 struct  </a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h2><a name="SECTION006260000000000000000">
4.2.6 例子</a>
</h2>

<p>
<div class="leftline" id="par98265" align="LEFT">
<strong>模拟 <tt class="cfunction">scanf()</tt></strong></div>

<p>
  当前Python没有一个相当于 <tt class="cfunction">scanf()</tt>。<a id='l2h-438' xml:id='l2h-438'></a>
  正则表达式通常更强大，但也更详细，比 <tt class="cfunction">scanf()</tt> 格式串。下面的表格提供了
<tt class="cfunction">scanf()</tt> 格式标记和正则表达式之间大体上的一些相当的映射。<p>
<div class="center"><table class="realtable">
  <thead>
    <tr>
      <th class="left"  ><tt class="cfunction">scanf()</tt> 标记</th>
      <th class="left"  >正则表达式</th>
      </tr>
    </thead>
  <tbody>
    <tr><td class="left"   valign="baseline"><code>%c</code></td>
        <td class="left"  ><tt class="regexp">.</tt></td></tr>
    <tr><td class="left"   valign="baseline"><code>%5c</code></td>
        <td class="left"  ><tt class="regexp">.{5}</tt></td></tr>
    <tr><td class="left"   valign="baseline"><code>%d</code></td>
        <td class="left"  ><tt class="regexp">[-+]?\d+</tt></td></tr>
    <tr><td class="left"   valign="baseline"><code>%e</code>, <code>%E</code>, <code>%f</code>, <code>%g</code></td>
        <td class="left"  ><tt class="regexp">[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?</tt></td></tr>
    <tr><td class="left"   valign="baseline"><code>%i</code></td>
        <td class="left"  ><tt class="regexp">[-+]?(0[xX][\dA-Fa-f]+|0[0-7]*|\d+)</tt></td></tr>
    <tr><td class="left"   valign="baseline"><code>%o</code></td>
        <td class="left"  ><tt class="regexp">0[0-7]*</tt></td></tr>
    <tr><td class="left"   valign="baseline"><code>%s</code></td>
        <td class="left"  ><tt class="regexp">\S+</tt></td></tr>
    <tr><td class="left"   valign="baseline"><code>%u</code></td>
        <td class="left"  ><tt class="regexp">\d+</tt></td></tr>
    <tr><td class="left"   valign="baseline"><code>%x</code>, <code>%X</code></td>
        <td class="left"  ><tt class="regexp">0[xX][\dA-Fa-f]+</tt></td></tr></tbody>
</table></div>

<p>

 从字符串中提取文件名和数字，像这样
<p>
<div class="verbatim"><pre>
    /usr/sbin/sendmail - 0 errors, 4 warnings
</pre></div>

<p>你使用<tt class="cfunction">scanf()</tt>格式，像这样
<p>
<div class="verbatim"><pre>
    %s - %d errors, %d warnings
</pre></div>

<p>
  相当的正则表达式应该是
<p>
<div class="verbatim"><pre>
    (\S+) - (\d+) errors, (\d+) warnings
</pre></div>

<p>
<div class="leftline" id="par98266" align="LEFT"><strong>避免递归</strong></div>

<p>
  如果你创建需要引擎执行大量递归的正则表达式，你可能遇到一个带有最大递归限制超出的信息的<tt class="exception">RuntimeError</tt>异常。例如，<p>
<div class="verbatim"><pre>
&gt;&gt;&gt; import re
&gt;&gt;&gt; s = 'Begin ' + 1000*'a very long string ' + 'end'
&gt;&gt;&gt; re.match('Begin (\w| )*? end', s).end()
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in ?
  File "/usr/local/lib/python2.5/re.py", line 132, in match
    return _compile(pattern, flags).match(string)
RuntimeError: maximum recursion limit exceeded
</pre></div>

<p>
  你可以经常重构你的正则表达式避免递归。

<p>
Python 2.3开始，<tt class="regexp">*?</tt> 模式的简单使用被特殊封装来避免递归。因此，上面的正则表达式可以通过改换为<tt class="regexp">Begin [a-zA-Z0-9_ ]*?end</tt>来避免递归。更多的好处，如正则表达式将比它们递归的等价物运行的更快。
<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="4.2.5 match Objects"
  href="match-objects.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="4.2 re  "
  href="module-re.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="4.3 struct  "
  href="module-struct.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python库参考</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">前一节:</b>
<a class="sectref" rel="prev" href="match-objects.html">4.2.5 匹配对象</a>
<b class="navlabel">上一级:</b>
<a class="sectref" rel="parent" href="module-re.html">4.2 re  </a>
<b class="navlabel">下一节:</b>
<a class="sectref" rel="next" href="module-struct.html">4.3 struct  </a>
</div>
</div>
<hr />
<span class="release-info">2.5.1 版本，文档更新在 2007，4，18。翻译：<a href="http://www.zkfarmer.org">zkfarmer</a>。</span></div>
<!--End of Navigation Panel-->
<address>
关于建议修改的信息，查阅 <i><a href="about.html">关于这个文档...</a></i> 。
</address>
</body>
</html>
